package com.ihrm.system.realm;

import com.ihrm.common.shiro.realm.IhrmRealm;
import com.ihrm.domain.system.po.User;
import com.ihrm.domain.system.vo.ProfileResult;
import com.ihrm.system.service.PermissionService;
import com.ihrm.system.service.UserService;
import org.apache.shiro.authc.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;

/**
 * @author VectorX
 * @version V1.0
 * @description
 * @date 2024-06-05 17:05:46
 */

public class UserRealm extends IhrmRealm implements Serializable
{
    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    // 重写认证方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        final UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;
        final String mobile = upToken.getUsername();
        final String password = String.valueOf(upToken.getPassword());
        final User user = userService.findByMobile(mobile);
        if (user.getPassword().equals(password)) {
            ProfileResult result = new ProfileResult(user);
            // 安全数据，密码，realm域名
            return new SimpleAuthenticationInfo(result, user.getPassword(), this.getName());
        }
        return null;
    }
}
